//
//  BYLoginView.m
//  ZhiHuiLiJiang
//
//  Created by 胡忠诚 on 2018/11/20.
//  Copyright © 2018 WisdomLijiang. All rights reserved.
//登录的View

#import "BYLoginView.h"

@interface BYLoginView()<UITextFieldDelegate>
/**返回按钮*/
@property(nonatomic,strong)UIButton *backBtn;
/**logo图*/
@property(nonatomic,strong)UIImageView *logoImgView;
/**区号*/
@property(nonatomic,weak)UILabel *phoneNumLab;
/**手机号*/
@property(nonatomic,weak)UITextField *phoneTF;
/**下划线*/
@property(nonatomic,weak)UIView *oneLineView;
/**验证码*/
@property (nonatomic, weak)UITextField *veriCodeTF;
/**下划线*/
@property(nonatomic,weak)UIView *twoLineView;
/**登录按钮*/
@property (nonatomic, weak)UIButton *loginBtn;
/**协议*/
//是否同意协议
@property(nonatomic,assign)BOOL isAgree;
@property (nonatomic, strong)UIButton *agreeBut;
@property (nonatomic, strong)UIButton *agreementBtn;

@end
@implementation BYLoginView
#pragma mark- init初始化
- (instancetype)init{
    self = [super init];
    if (self) {
        [self initAddSubViews];
    }
    return self;
}
- (void)initAddSubViews{
    //返回按钮
    _backBtn = [[UIButton alloc]init];
    [_backBtn setImage:[UIImage imageNamed:@"mainIcon_back"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backBtn];
    
    //logo图
    _logoImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mineIcon_LoginLOGO"]];
    [self addSubview:_logoImgView];
   
    //区号
    _phoneNumLab = [BYVS createLabText:@"+86" font:BYPTFont36 labColor:BYColor5 addSubView:self];
    [_phoneNumLab sizeToFit];
    
    //手机号
    _phoneTF = [BYVS CreateNorTFWithKBType:UIKeyboardTypeNumberPad placeholder:@"请输入手机号" tiFont:BYPTFont36 textColor:BYColor5 addSubView:self];
    _phoneTF.textAlignment = NSTextAlignmentLeft;
    _phoneTF.delegate = self;
    [_phoneTF addTarget:self action:@selector(reTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //下划线
    _oneLineView = [BYVS createViewBGColor:BYColor6 addSubView:self];
    
    //验证码
    _veriCodeTF = [BYVS CreateNorTFWithKBType:UIKeyboardTypeNumberPad placeholder:@"请输入短信验证码" tiFont:BYPTFont36 textColor:BYColor5 addSubView:self];
    _veriCodeTF.textAlignment = NSTextAlignmentLeft;
    _veriCodeTF.delegate = self;
    [_veriCodeTF addTarget:self action:@selector(reTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //发送验证码
    _sendVeriCodeBtn = [[BYVerificationCode alloc]init];
    [self addSubview:_sendVeriCodeBtn];
    [_sendVeriCodeBtn addTarget:self action:@selector(sendVeriCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //下划线
    _twoLineView = [BYVS createViewBGColor:BYColor6 addSubView:self];
    
    //登录按钮
    _loginBtn = [BYVS createBtnBGColor:BYGrayColor title:@"登录" titleColor:BYColor3 addSubView:self];
    _loginBtn.layer.cornerRadius = BY_Width(16);
    _loginBtn.layer.masksToBounds = YES;
    [_loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn.enabled = NO;
    
    //协议
    _agreeBut = [BYVS createBtnNorIcon:@"mineIcon_choice_D" selIcon:@"mineIcon_choiceRed_S" addSubView:self];
    _agreeBut.selected = self.isAgree;
    [_agreeBut addTarget:self action:@selector(agreeButClick:) forControlEvents:UIControlEventTouchUpInside];
    //同意协议
    _agreeBut.selected = YES;
    self.isAgree = YES;
    
    //文字协议
    _agreementBtn = [[UIButton alloc]init];
    _agreementBtn.titleLabel.font = BYPTFont26;
    _agreementBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    NSMutableAttributedString *btnStr = [[NSMutableAttributedString alloc] initWithString:@"我已阅读并同意智慧丽江协议"];
    NSRange btnRange = {0,[btnStr length]};
    [btnStr addAttribute:NSForegroundColorAttributeName value:BYColor7 range:NSMakeRange(0, btnRange.length-6)];
    [btnStr addAttribute:NSForegroundColorAttributeName value:BYColor5 range:NSMakeRange(btnRange.length-6, 6)];
    [btnStr addAttribute:NSFontAttributeName value:BYPTBoldFont26 range:NSMakeRange(btnRange.length-6, 6)];//字体加粗
    [_agreementBtn setAttributedTitle:btnStr forState:UIControlStateNormal];
    [_agreementBtn addTarget:self action:@selector(agreementBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_agreementBtn];
    
    [self addSubViewFrames];
}
- (void)addSubViewFrames{
    BY_kWeakSelf(ws);
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {//返回按钮
        make.left.equalTo(ws.mas_left).offset(BY_Width(24));
        make.top.equalTo(ws.mas_top).offset(BY_Height(28));
        make.width.offset(BY_Width(36));
        make.height.offset(BY_Height(31));
    }];
    [_logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {//logo图
        make.top.equalTo(ws.mas_top).offset(BY_Height(192));
        make.centerX.equalTo(ws.mas_centerX);
        make.width.offset(BY_ScreenWidth - BY_Width(144*2));
        make.height.offset(BY_Height(148));
    }];
    [_oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {//下划线
        make.left.equalTo(ws.mas_left).offset(BY_Width(60));
        make.right.equalTo(ws.mas_right).offset(BY_Width(-69));
        make.top.equalTo(ws.logoImgView.mas_bottom).offset(BY_Height(148)+BY_Height(80));
        make.height.offset(BY_Height(1));
    }];
    [_phoneNumLab mas_makeConstraints:^(MASConstraintMaker *make) {//区号
        make.left.equalTo(ws.oneLineView.mas_left);
        make.bottom.equalTo(ws.oneLineView.mas_top);
        make.height.offset(BY_Height(80));
    }];
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {//手机号
        make.left.equalTo(ws.phoneNumLab.mas_right).offset(BY_Width(32));
        make.width.offset(BY_ScreenWidth - BY_Width(60)-BY_Width(32)-BY_Width(50)-BY_Width(69));
        make.centerY.equalTo(ws.phoneNumLab.mas_centerY);
        make.height.equalTo(ws.phoneNumLab.mas_height);
    }];
    [_twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {//下划线2
        make.left.right.height.equalTo(ws.oneLineView);
        make.top.equalTo(ws.oneLineView.mas_bottom).offset(BY_Height(80));
    }];
    [_veriCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {//验证码
        make.left.equalTo(ws.twoLineView.mas_left);
        make.height.offset(BY_Height(80));
        make.bottom.equalTo(ws.twoLineView.mas_top);
        make.width.offset(BY_ScreenWidth - BY_Width(60)-BY_Width(250)-BY_Width(69));
    }];
    [_sendVeriCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {//发送验证码
        make.top.height.equalTo(ws.veriCodeTF);
        make.left.equalTo(ws.veriCodeTF.mas_right);
        make.right.equalTo(ws.twoLineView.mas_right).offset(BY_Width(-5));
    }];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {//登录按钮
        make.left.equalTo(ws.mas_left).offset(BY_Width(68));
        make.width.offset(BY_ScreenWidth - BY_Width(68)*2);
        make.top.equalTo(ws.twoLineView.mas_bottom).offset(BY_Height(68));
        make.height.offset(BY_Height(80));
    }];
    [_agreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {//协议勾选按钮
        make.right.equalTo(ws.mas_right).offset(BY_Width(-188));
        make.bottom.equalTo(ws.mas_bottom).offset(BY_Height(-44));
    }];
    [_agreeBut mas_makeConstraints:^(MASConstraintMaker *make) {//协议文字
        make.right.equalTo(ws.agreementBtn.mas_left).offset(BY_Width(-10));
        make.width.height.offset(BY_Width(30));
        make.centerY.equalTo(ws.agreementBtn.mas_centerY);
    }];
}
#pragma mark- textField处理
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hideKeyBoard];
}
- (void)reTextFieldDidChange:(UITextField *)textField{
    //不允许输入空格
    textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (_phoneTF.text.length > 0 && _veriCodeTF.text.length > 0) {
        _loginBtn.enabled = YES;
        _loginBtn.backgroundColor = BYColor4;
        [_loginBtn setTitleColor:BYColor3 forState:UIControlStateNormal];
    }else{
        _loginBtn.enabled = NO;
        _loginBtn.backgroundColor = BYGrayColor;
        [_loginBtn setTitleColor:BYColor6 forState:UIControlStateNormal];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ //设置文本框最大长度
    if (textField == _phoneTF) {
        return (textField.text.length - range.length + string.length) <= 11;
    }else{
        return (textField.text.length - range.length + string.length) <= 6;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self hideKeyBoard];
    return YES;
}
- (void)hideKeyBoard{
    [self endEditing:YES];
}
- (void)sendVeriCodeBtnClick{//点击了发送验证码按钮
    [self hideKeyBoard];
    if (![[BYRegular checkPhone:_phoneTF.text] isEqualToString:@""]) {//手机号排空及校验
        [UIView addMJNotifierWithText:[BYRegular checkPhone:_phoneTF.text] dismissAutomatically:YES];
        return;
    }
    if (self.clickSendLoginVeriCodeBtn) {
        _sendVeriCodeBtn.enabled = NO;
        self.clickSendLoginVeriCodeBtn(_phoneTF.text);
    }
}
- (void)backBtnClick:(UIButton *)btn{//点击了返回按钮
    if (self.clickBackBtn) {
        self.clickBackBtn();
    }
}
- (void)loginBtnClick{//点击了登录按钮
    [self hideKeyBoard];
    if (![[BYRegular checkPhone:_phoneTF.text] isEqualToString:@""]) {//手机号排空及校验
        [UIView addMJNotifierWithText:[BYRegular checkPhone:_phoneTF.text] dismissAutomatically:YES];
        return;
    }
    if (![[BYRegular checkVeriCode:_veriCodeTF.text] isEqualToString:@""]) {//验证码排空及校验
        [UIView addMJNotifierWithText:[BYRegular checkVeriCode:_veriCodeTF.text] dismissAutomatically:YES];
        return;
    }
    if (!self.isAgree) {
        [UIView addMJNotifierWithText:@"请同意服务协议" dismissAutomatically:YES];
        return;
    }
    NSDictionary *paramsDic = @{@"mobile":_phoneTF.text,
                                @"sms":_veriCodeTF.text,
                                };
    if (self.clickLoginBtn) {
        self.clickLoginBtn(paramsDic);
    }
}
#pragma mark- 服务协议的勾选框
- (void)agreeButClick:(UIButton *)sender{
    self.isAgree = !self.isAgree;
    sender.selected = !sender.selected;
}
- (void)agreementBtnClick{//点击了协议按钮
    if (self.clickAgreementBtn) {
        self.clickAgreementBtn();
    }
}


@end
